<?php
namespace Home\Controller;
use Home\Biz\PromoterBiz;
use Think\Controller;
use Home\Biz\StringBiz;
use Home\Biz\OrderBiz;

class OrderController extends LoginBaseController {
	/**
	 * 订单列表显示
	 */
	public function orderList(){
		$this->assign('module_id',	'order');
		$this->assign('page_id',	'orderList');
		$this->assign('page_name',	'推广订单列表');
		
		
		$this->display('order/list');
    }
    
    
    /**
     * 订单详情页面
     */
    public function detail(){
    	$order_id = I('order_id');
    	
    	
    	$promoterOrder_en = M('promoterOrder')->where("order_id='%s'", $order_id)->find();
    	$promoterOrder_en['order_status_text'] = StringBiz::getPromoterOrderStatusText($promoterOrder_en['order_status']);
    	$promoterOrder_en['order_revenue_status_text'] = StringBiz::getPromoterOrderRevenueStatusText($promoterOrder_en['order_revenue_status']);
    	$promoterOrder_en['payment_status_text'] = StringBiz::getPaymentStatusText($promoterOrder_en['payment_status']);
    	$this->assign('promoterOrder_en', $promoterOrder_en);
    	
    	$lst_promoter_order_revenue_detail_en	= M('promoterOrderRevenueDetail')->where("order_id='%s'", $order_id)->select();
    	foreach($lst_promoter_order_revenue_detail_en as $key => $revenue_detail_en){
    		$lst_promoter_order_revenue_detail_en[$key]['promoter_type_text']	= StringBiz::getPromoterTypeText($lst_promoter_order_revenue_detail_en[$key]['promoter_type']);
    		$lst_promoter_order_revenue_detail_en[$key]['revenue_type_text']	= StringBiz::getRevenueTypeText($lst_promoter_order_revenue_detail_en[$key]['revenue_type']);
    		
    		if($lst_promoter_order_revenue_detail_en[$key]['promoter_type'] == 1){
    			$lst_promoter_order_revenue_detail_en[$key]['revenue_base_amount']	= StringBiz::getPromoterTypeText($lst_promoter_order_revenue_detail_en[$key]['person_commission_amount']);
    		}
    		else{
    			$lst_promoter_order_revenue_detail_en[$key]['revenue_base_amount']	= StringBiz::getPromoterTypeText($lst_promoter_order_revenue_detail_en[$key]['dealer_commission_amount']);
    		}
    		
    	}
    	$this->assign('lst_promoter_order_revenue_detail_en', $lst_promoter_order_revenue_detail_en);

        //region 控制页面按钮的显示
        //1.【确认订单已收货】按钮
        $is_show_button_reviceGoods = 0;
        if($promoterOrder_en['payment_status'] == 1){
            $is_show_button_reviceGoods = 1;
        }
        $this->assign('is_show_button_reviceGoods',	$is_show_button_reviceGoods);
        //endregion 控制页面按钮的显示
    	
    	
    	$this->assign('module_id',	'order');
    	$this->assign('page_id',	'orderDetail');
    	$this->assign('page_name',	'订单分成信息');
    	$this->display('order/detail');
    }
    
    
    
    public function stockOrderImport(){
    	
    	$this->assign('module_id',	'order');
    	$this->assign('page_id',	'stockOrderImport');
    	$this->assign('page_name',	'出库单导入');
    	$this->display('order/stockOrderImport');
    }
    
    public function anx(){
    	
    	OrderBiz::stockOrderImport('./Public/Uploads/stockOrderImport/stockOrderImport.xls');
    }
    
    public function stockOrderImportAction(){
    	$upload_config = array(
    			'maxSize'	=> 0,
    			'rootPath'	=> './Public/Uploads/',
    			'savePath'	=> 'stockOrderImport/',
    			'saveName'	=> 'stockOrderImport',
    			'exts'		=> array('xls'),
    			'subName' => '',
    			'replace' => true,
    	);
        $upload->autoSub = false;
    	$upload = new \Think\Upload($upload_config); // 实例化上传类

    	
    	$info = $upload->upload();
    	if (!$info) {
    		// 上传错误提示错误信息
    		redirect('stockOrderImportResult?msg=上传失败！'.$upload->getError());
    	} else {
    		// 上传成功
    		//读取csv文件将相关数据写入数据库
    		$err_msg = OrderBiz::stockOrderImport('./Public/Uploads/stockOrderImport/stockOrderImport.xls');
    		if(empty($err_msg)){
    			redirect('stockOrderImportResult?msg=已成功上传并处理！');
    		}
    		else{
    			redirect("stockOrderImportResult?msg=$err_msg！");
    		}
    		
    	}
    }
    
    
    public function stockOrderImportResult(){
    	$this->assign('msg', I('msg'));    	
		$this->display('order/stockOrderImportResult');
    }
    

    //region ========= Action页面(各个页面的button事件) =========

    /**
     * 订单详情页 【确认订单已完成】按钮
     * /order/detail
     */
    public function OrderReciveGoodsAction(){
        $order_id   = I('order_id');

        #region 1、参数校验
        $promoterOrder_en = M('promoterOrder')->where("order_id='%s'", $order_id)->find();
        if($promoterOrder_en['payment_status'] != 1){
            $info_msg   = '无法完成订单，订单'.$order_id.'尚未支付！';
            redirect ( C ( 'AdminConfig.error_page' ) . '?type=404&is_show_button=2&msg=' . $info_msg );
        }
        if($promoterOrder_en['order_status'] == 35){
            $info_msg   = '无法完成订单，订单'.$order_id.'已经完成！';
            redirect ( C ( 'AdminConfig.error_page' ) . '?type=404&is_show_button=2&msg=' . $info_msg );
        }
        #endregion 1、参数校验


        #region 2、完成订单
        M('promoter_order')->where("order_id='%s'", $order_id)->setField('order_status', 35);
        M('order_info', '', 'DB_trade')->where("order_id='%s'", $order_id)->setField('order_status', 35);
        #endregion 2、完成订单

        redirect ( C ( 'AdminConfig.info_page' ) . '?type=404&is_show_button=2&msg=订单'.$order_id.'已成功完成！');
    }

    /**
     * 为所有符合条件的订单进行返现与可提现余额
     */
    public function OrderRevenueAction(){
        $revenue_count          = PromoterBiz::revenueAll();
        $ConvertWithdraw_count  = PromoterBiz::ConvertWithdrawAll();

        $msg    = "已成功返现".$revenue_count.'张订单。已成功将'.$ConvertWithdraw_count.'笔余额转入可提现余额。';
        redirect ( C ( 'AdminConfig.info_page' ) . '?type=404&is_show_button=2&msg='.$msg);
    }
    //endregion ========= Action页面(各个页面的button事件) =========


    /**
     * 东方有线发货单导出
     */
    public function DFYXshipOrderExcel(){
    	$params_en = array (
    			'dealer_id' 		=> 'DL1739763',
    			'order_status'		=> 22,
    			'payment_status'	=> 1,
    	);
    	
    	Vendor('phpExcel.PHPExcel');
    	$objPHPExcel = OrderBiz::DFYXshipOrderExcel($params_en);
    	
    	$objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);
    	header("Pragma: public");
    	header("Expires: 0");
    	header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
    	header("Content-Type:application/force-download");
    	header("Content-Type:application/vnd.ms-execl");
    	header("Content-Type:application/octet-stream");
    	header("Content-Type:application/download");
    	header('Content-Disposition:attachment;filename="resume.xls"');
    	header("Content-Transfer-Encoding:binary");
    	$objWriter->save('php://output');
    }

    /**
     * 等级品发货单导出
     */
    public function DJPshipOrderExcel(){
        $params_en = array (
            'payment_status'	=> 1,
        );

        Vendor('phpExcel.PHPExcel');
        $objPHPExcel = OrderBiz::DJPshipOrderExcel($params_en);

        $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        header('Content-Disposition:attachment;filename="resume.xls"');
        header("Content-Transfer-Encoding:binary");
        $objWriter->save('php://output');
    }

    
    public function shipOrderExcel(){
    	//参数处理
    	$params_en = array (
    			'promoter_id' 		=> I ( 'promoter_id' ),
    			'promoter_name' 	=> I ( 'promoter_name' ),
    			'order_id' 			=> I ( 'order_id' ),
    			'order_status'		=> I('order_status'),
    			'payment_status'	=> I('payment_status'),
    			'begin_order_date'	=> urldecode(I('begin_order_date')),
    			'end_order_date'	=> urldecode(I('end_order_date')),
    	);
    	
    	Vendor('phpExcel.PHPExcel');
    	$objPHPExcel = OrderBiz::shipOrderExcel($params_en);
    	
    	//输出到浏览器
    	//$objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
    	$objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);

    	header("Pragma: public");
    	header("Expires: 0");
    	header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
    	header("Content-Type:application/force-download");
    	header("Content-Type:application/vnd.ms-execl");
    	header("Content-Type:application/octet-stream");
    	header("Content-Type:application/download");
    	header('Content-Disposition:attachment;filename="resume.xls"');
    	header("Content-Transfer-Encoding:binary");
    	$objWriter->save('php://output');
    }
    
}